﻿using STHPMS1.BLL.B_Core;
using STHPMS1.ICore.I_BLL.Bus;
using STHPMS1.ICore.I_Entity.Info;
using STHPMS1.ICore.I_Entity.Stmpms1;
using System;
using System.Collections.Generic;
using System.Linq.Expressions;
using STHPMS1.ICore.I_DAL;
namespace STHPMS1.BLL.B_Bus
{
    public class S_Pmds_CemBus : BllBase, IS_Pmds_CemBill
    {
        public void Save<T>(T entity)
            where T : class ,IS_Pmds_Cem
        {
            if (this.Scene.Dal.Sthpms1.Update(entity) == 0)
            {
                this.Scene.Dal.Sthpms1.Insert(entity);
            }
        }

        public void Remove(IS_Pmds_Cem entity)
        {
            this.Scene.Dal.Sthpms1.Delete(entity);
        }

        public IEnumerable<T> Get<T>(Expression<Func<T, bool>> predicate, IPagingInfo info)
            where T : class, IS_Pmds_Cem
        {
            return this.Scene.Dal.Sthpms1.Select<T>(predicate, info);
        }


        public void DelByInspecId(string inspecId)
        {
            this.Scene.Dal.Sthpms1.Delete<IS_Pmds_Cem>(new { Inspecid = inspecId });
        }

        public string Exists(string unit, string inspecid)
        {
            ISthpms1Dao dao = this.Scene.Dal.Sthpms1;
            List<DateTime> h = dao.Column<DateTime>(dao.From<IS_Pmds_Cem>().Select(x => x.Svydata)
                         .Where(q => q.Unit == unit && q.Inspecid == inspecid).OrderByDescending(y => y.Svydata));
            if (h.Count > 0)
            {
                return h[0].ToShortDateString();
            }
            return "";
        }
    }
}
